package com.yiwyn.example;


import lombok.extern.slf4j.Slf4j;
import org.openjdk.jmh.annotations.*;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.RunnerException;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;

import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;

@Slf4j
@Measurement(iterations = 2, time = 1)
@Warmup(iterations = 1, time = 1)
@BenchmarkMode(Mode.AverageTime)
@Fork(1)
@State(Scope.Group)
public class QueueTest {

    private Queue<Integer> queue = new ConcurrentLinkedQueue<>();  // 组内共享队列

    @Benchmark
    @Group("consume")
    public void produce() {
        queue.add(1);
        log.info("queue:{}", queue);
    }

    @Benchmark
    @Group("consume")
    public Integer consume() {
        Integer poll = queue.poll();
        log.info("poll:{}", poll);
        return poll;
    }


    public static void main(String[] args) throws RunnerException {
        Options options = new OptionsBuilder()
                .include(QueueTest.class.getSimpleName())
                // .addProfiler(GCProfiler.class)
                // .jvmArgs("-XX:+PrintCompilation")
                .build();

        new Runner(options).run();
    }

}
